(function() {
  var apis = {
    upload: 'http://127.0.0.1:2009/file/upload/',
    list: 'http://127.0.0.1:2009/file/list/',
    folder: 'http://127.0.0.1:2009/folder/'
  };

  var folderPath = [];
  Path.on('/main/:__basename__.html', function(__current_location) {

    var qs = __current_location.query;
    folderPath = [];

    var folder = qs.get('folder');

    if (folder) {
      App.set('$Cache.main.current_folder', folder);
      getFolderPath(folder);
    }

    coAjax.get(apis.list + (folder || ''), function(list) {
      App.set('$Cache.main.file.list', list);
      console.log(list);
    });


    require(['/js/tools/xhr.js'], function(xhr) {

      App.set('$Event.main.upload', function() {
        let form = new FormData();
        form.append('file', this.files[0]);
        xhr(apis.upload + (folder || ''), {
          method: 'POST',
          body: form
        }).then(x => {
          x.upload.onprogress = function(e) {
            console.log(e);
          };
          return x.json();
        }).then(json => {
          App.unshift('$Cache.main.file.list', json);
          console.log(json);
        });
      });
    });
  });

  function getFolderPath(folder) {
    coAjax.get(apis.folder + folder, function(father) {
      folderPath.unshift(father);
      if (father.folder) return getFolderPath(father.folder);

      App.set('$Cache.main.folderFullPath', folderPath);
    });
  }


  App.set('$Event.main.change_list_type', function() {
    let data = this.getAttribute('data');
    if (data == 'list')
      App.set('$Cache.main.list_type', true);
    else
      App.set('$Cache.main.list_type', false);

  });





}());
